package com.ykdl.member.kid.set;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ConsigneeBean;
import com.ykdl.member.kid.beans.ConsigneeListBean;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.beans.StoreBean;
import com.ykdl.member.kid.beans.Store_ListBean;
import com.ykdl.member.kid.beans.SystemMessageBean;
import com.ykdl.member.kid.circle.LookImageActivity;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class MyAddressActivity extends BaseScreen{
	private RefreshListView myaddress_listview ;
	private MyAddressAdapter adapter;
	private Map<Integer, Boolean> isSelected;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("地址管理", 0, R.drawable.back_bg, new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		}, 0,
		R.drawable.add__bt_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				go(AddAddressActivity.class);
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.myaddressactivity_xml);
		inint();
	}
	public void inint() {
		adapter = new MyAddressAdapter(this);
		myaddress_listview = (RefreshListView)findViewById(R.id.myaddress_listview);
		myaddress_listview.setMoreButtoIsGon(true);
		myaddress_listview.setAdapter(adapter);
		myaddress_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ConsigneeBean bean =(ConsigneeBean)adapter.getItem(arg2);
				Intent intent = new Intent(MyAddressActivity.this, EditAddressActivity.class);
				intent.putExtra("ConsigneeBean",  (Serializable)bean);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				MyAddressActivity.this.startActivity(intent);
			}
		});
		
		getData();
	}
	public void getData() {
		showProgress("加载中请稍等...");
		String  pageurl =KidConfig.GET_CONSIGNEE_LIST;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("cursor", "0");
		parameterss.put("count", "500");
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl,parameterss);
		 TaskManager.startHttpRequest(request, new getDataTag(),  ConsigneeListBean.class);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ConsigneeListBean) {
				ConsigneeListBean bean = (ConsigneeListBean)t;
				if(null==bean.getError()){
					if (bean.getConsignee_list().size()>0) {
					adapter.setlist(bean.getConsignee_list());
					myaddress_listview.setVisibility(View.VISIBLE);
						myaddress_listview.setAdapter(adapter);
						adapter.notifyDataSetChanged();
						hidErrorTip( );
						
					}else {
						myaddress_listview.setVisibility(View.GONE);
						showErrorTip("没有数据","");
					}
				myaddress_listview.onRefreshComplete();
				myaddress_listview.setFecthMoreOk();
				}else {
					Toast.makeText(mContext, "失败,"+bean.getDesc(), 1).show();
				}
				}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	class MyAddressAdapter extends BaseAdapter{
		private ArrayList<ConsigneeBean>   list;
		private Context mContext ;
		public MyAddressAdapter(Context context){
			mContext = context;
		}
		public void setlist(ArrayList<ConsigneeBean>  list) {
			this.list = list ;
			isSelected = new HashMap<Integer, Boolean>();  
	        for (int i = 0; i < list.size(); i++) {  
	        		 isSelected.put(i, list.get(i).isIs_default()); 
	        		 
	        }  
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position-1);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position1, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			 final int position = position1;  
			if (convertView == null) {
				convertView = RelativeLayout.inflate(mContext,
						R.layout.myaddressadapter_item, null);
				holder = new ViewHolder();
				holder.is_check = (CheckBox) convertView.findViewById(R.id.is_check);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.phone = (TextView) convertView
						.findViewById(R.id.phone);
				holder.adress = (TextView) convertView.findViewById(R.id.adress);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.name.setText(list.get(position).getName());
			holder.phone.setText(list.get(position).getPhone());
			holder.adress.setText(list.get(position).getArea()+" "+list.get(position).getAddress());
			holder.is_check.setTag(list.get(position));
			holder.is_check.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    // 当前点击的CB  
                	ConsigneeBean bean = (ConsigneeBean)v.getTag();
						setDefaultData(bean.getConsignee_id(),position);
                }  
            });  
//			holder.is_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//				@Override
//				public void onCheckedChanged(CompoundButton buttonView,
//						boolean isChecked) {
//					// TODO Auto-generated method stub
//					if (isChecked) {
//						 for(Integer p : isSelected.keySet()) {  
//		                        isSelected.put(p, false);  
//		                    } 
//						 isSelected.put(position, true);
//					}else {
//						 for(Integer p : isSelected.keySet()) {  
//		                        isSelected.put(p, false);  
//		                    } 
//					}
//                      
//                    MyAddressAdapter.this.notifyDataSetChanged();  
//				}});
			holder.is_check.setChecked(isSelected.get(position));
			return convertView;
		}
		class ViewHolder {
			CheckBox is_check;
			TextView name;
			TextView phone;
			TextView adress;
		}
	}
	public void setDefaultData(String consignee_id,int position) {
		showProgress("请稍等...");
		String  pageurl =KidConfig.SETDEFAULT_CONSIGNEE;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("consignee_id", consignee_id);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new setDefaultDataTag( position),  ResultBean.class);
	}
	
	class setDefaultDataTag implements ITag {
		private int position;
		public setDefaultDataTag(int position) {
			// TODO Auto-generated constructor stub
			this.position = position ;
		}

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean = (ResultBean)t;
				if(null==bean.getError()){
					Toast.makeText(mContext, "设置成功", 1).show();
					 boolean cu = !isSelected.get(position);  
	                    // 先将所有的置为FALSE  
	                    for(Integer p : isSelected.keySet()) {  
	                        isSelected.put(p, false);  
	                    }  
	                    // 再将当前选择CB的实际状态  
	                    isSelected.put(position, cu);  
	                    adapter.notifyDataSetChanged();  
				}else {
                    adapter.notifyDataSetChanged();  
					Toast.makeText(mContext, bean.getDesc(), 1).show();
				}
				}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
}
